﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Members;

namespace VK.Casemeet.Web.Front
{
    public partial class MemberRegister : System.Web.UI.Page
    {
        public string strMessage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        protected void butSubmit_Click(object sender, EventArgs e)
        {
            if (txtEmail.Text.Length == 0)
            {
                strMessage += "<p>帐号不能为空!</p>";
            }
            else
            {
                if (!RegexStr.CheckEmail(txtEmail.Text))
                {
                    strMessage += "<p>帐号只能为邮箱格式!</p>";
                }
            }
            if (txtPass.Text.Length == 0)
            {
                strMessage += "<p>密　码不能为空!</p>";
            }
            else
            {
                if (txtPass.Text.Length < 4 || txtPass.Text.Length > 20)
                {
                    strMessage += "<p>密　码(4-20)位字符!</p>";
                }
                else
                {
                    if (!string.Equals(txtPass.Text, txtPassCheck.Text))
                    {
                        strMessage += "<p>两次密码输入要相同!</p>";
                    }
                }
            }
            if (txtNickname.Text.Length == 0)
            {
                strMessage += "<p>昵　称不能为空!</p>";
            }
            else
            {
                if (txtNickname.Text.Length < 2 || txtPass.Text.Length > 20)
                {
                    strMessage += "<p>昵　称(2-20)位字符!</p>";
                }
                else
                {
                    if (MemberBLL.GetMemberByEmail(txtNickname.Text) != null)
                    {
                        strMessage += "<p>昵称重复，请更换昵称!</p>";
                    }
                }
            }
            if (txtCode.Text.Trim().Length == 0)
            {
                strMessage += "<p>验证码不能为空!</p>";
            }
            else
            {
                if (Util.GetCookieString("AdminValidateCode") != txtCode.Text.ToLower())
                {
                    strMessage += "<p>验证码输入错误!</p>";
                }
            }

            if (!string.IsNullOrEmpty(strMessage))
            {
                strMessage = "<dd id='vcodeMsg' class='regMsg'>" + strMessage;
                strMessage = strMessage + "</dd>";
                return;
            }

            MemberEntity member = LoadEntity();

            switch (MemberBLL.Register(member))
            {
                case 1: Response.Redirect("/ActiveeMail.html"); break;
                case 3: strMessage = "<dd id='vcodeMsg' class='regMsg'><p>请更换帐号，此帐号已有人使用!</p></dd>"; break;
            }
        }

        protected MemberEntity LoadEntity()
        {
            MemberEntity member = new MemberEntity();
            member.Email = txtEmail.Text;
            member.Password = DES.Encrypt(txtPass.Text, CS.MemberPassKey);
            member.Nickname = txtNickname.Text;
            member.Name = string.Empty;
            member.Sex = 0;
            member.Birthday = Util.ConverDateTime(DBNull.Value);
            member.QQ = string.Empty;
            member.Phone = string.Empty;
            member.ProvinceID = 0;
            member.CityID = 0;
            member.AreaID = 0;
            member.Address = string.Empty;
            member.Profession = 0;
            member.Education = 0;
            member.Income = 0;
            member.Marriage = 0;
            member.LandingNumber = 1;
            member.LandDateTime = DateTime.Now;
            member.Created = DateTime.Now;
            member.State = 1;
            member.ValidateCode = Guid.NewGuid().ToString();

            return member;
        }
    }
}